package cn.shyouchi.wuguanjia.android;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.miguelbcr.ui.rx_paparazzo.RxPaparazzo;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.MultiActionsNotificationBuilder;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.ui.attendance.AttendanceActivity;
import cn.shyouchi.wuguanjia.android.ui.event.EventManagerActivity;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.main.HomeModule;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.casual.CasualMainActivity;
import cn.shyouchi.wuguanjia.android.ui.meetting.MeettingMainActivity;
import cn.shyouchi.wuguanjia.android.ui.patrol.PatrolActivity;
import cn.shyouchi.wuguanjia.android.ui.patrolstatistic.PatrolStatisticManageActivity;
import cn.shyouchi.wuguanjia.android.ui.report.SelPostReportActivity;
import cn.shyouchi.wuguanjia.android.ui.reportstatistic.ReportStatisticManageActivity;
import cn.shyouchi.wuguanjia.android.ui.statistic.SignStatisticManagerActivity;
import cn.shyouchi.wuguanjia.android.ui.train.TrainMainActivity;
import cn.shyouchi.wuguanjia.android.ui.trainstatistic.SelTrainEvenActivity;
import cn.shyouchi.wuguanjia.android.ui.workshow.WorkShowMainActivity;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import io.realm.DynamicRealm;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;

/**
 * Created by Eenie on 2016/10/12 at 14:30
 * Des:
 */

public class App extends Application {


    RealmConfiguration.Builder builder;

    @Override
    public void onCreate() {
        super.onCreate();
        ToastUtil.register(this);
        RxPaparazzo.register(this);
        JPushInterface.init(this);
        builder = new RealmConfiguration.Builder(this);
        UserStorageProvider.register(this);
        MultiActionsNotificationBuilder notificationBuilder = new MultiActionsNotificationBuilder(this);
        JPushInterface.initCrashHandler(this);
        JPushInterface.setPushNotificationBuilder(2, notificationBuilder);
        builder.name("youchi.realm")
                .migration(new RealmMigration() {
                    @Override
                    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
                        realm.delete(HomeModule.class.getSimpleName());
                    }
                })
                .initialData(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        saveModule(realm);
                    }
                })
                .schemaVersion(2);
        Realm.setDefaultConfiguration(builder.build());
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    private void saveModule(Realm realm) {
        List<HomeModule> mModules = new ArrayList<>();
        mModules.add(new HomeModule(AttendanceActivity.class, "ic_home_module_sign", "考勤", false, 0, true));
        mModules.add(new HomeModule(SignStatisticManagerActivity.class, "ic_home_module_stati", "考勤统计", false, 1, true));
        mModules.add(new HomeModule(PatrolActivity.class, "ic_home_module_polling", "巡检", false, 2, true));
        mModules.add(new HomeModule(PatrolStatisticManageActivity.class, "ic_home_module_polling_stati", "巡检统计", false, 3, true));
        mModules.add(new HomeModule("ic_home_module__form_up", "表单上传", false, 4, true));
        mModules.add(new HomeModule("ic_home_module_visitor", "访客通行", false, 5, true));
        mModules.add(new HomeModule(MeettingMainActivity.class, "ic_home_module_meeting", "会议", false, 6, true));
        mModules.add(new HomeModule(EventManagerActivity.class, "ic_home_module_event_info", "项目设置", false, 7, true));
        mModules.add(new HomeModule("ic_home_module_fix", "维修", false, 10, false));
        mModules.add(new HomeModule(TrainMainActivity.class, "ic_home_module_train", "培训", false, 11, false));
        mModules.add(new HomeModule(WorkShowMainActivity.class, "ic_home_module_workshow", "工作秀", false, 12, false));
        mModules.add(new HomeModule(SelPostReportActivity.class, "ic_home_module_report", "报岗", false, 13, false));
        mModules.add(new HomeModule(ReportStatisticManageActivity.class, "ic_home_module_reportstatistic", "报岗统计", false, 14, false));
        mModules.add(new HomeModule(CasualMainActivity.class, "ic_home_module_casual", "随手拍", false, 15, false));
        mModules.add(new HomeModule(SelTrainEvenActivity.class, "ic_home_module_reportstatistic", "培训统计", false, 16, false));
        realm.insertOrUpdate(mModules);
    }


//    /**
//     * 初始化下载管理
//     */
//    private void initDownloaderManager() {
//        //初始化下载管理
//        //数据库表扩展字段，开发者可以添加一些扩展字段来满足业务需求，注意不能包含id,path,url，因为框架内部有定义不得重复
//        Map<String, String> dbExFeildMap = new HashMap<>();
//        dbExFeildMap.put("title", "VARCHAR");
//        dbExFeildMap.put("icon", "VARCHAR");
//
//        //下载文件所保存的目录
//        File storeFile = StorageUtils.getCacheDirectory(this, false, "FileDownloader");
//        if (!storeFile.exists()) {
//            storeFile.mkdirs();
//        }
//
//        final DownloaderManagerConfiguration.Builder dmBulder = new DownloaderManagerConfiguration.Builder(this)
//                .setMaxDownloadingCount(3) //配置最大并行下载任务数，配置范围[1-100]
//                .setDbExtField(dbExFeildMap) //配置数据库扩展字段
//                .setDbVersion(1)//配置数据库版本
//                .setAutoRetryTimes(5)
//                .setDbUpgradeListener(dbUpgradeListener) //配置数据库更新回调
//                .setDownloadStorePath(storeFile.getAbsolutePath()); //配置下载文件存储目录
//
//        //以上都是可选配置，如果开发者想使用默认配置可以DownloaderManagerConfiguration.Builder dmBulder = new DownloaderManagerConfiguration.Builder(this);即可
//
//        //初始化下载管理,最好放到线程去执行，因为里面有操作数据库的可能会引起卡顿
//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
//                DownloaderManager.getInstance().init(dmBulder.build());//必要语句
//            }
//        }.start();
//    }
//
//    /**
//     * 更新数据库监听
//     */
//    private DbUpgradeListener dbUpgradeListener = new DbUpgradeListener() {
//        @Override
//        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//
//        }
//    };


}
